#!/usr/bin/python
#coding:utf-8

import os
from xml.dom.minidom import Document

doc = Document()

class Node:
    def __init__(self, name, path, isdir, level):
        self.name = name
        self.path = path
        self.isdir = isdir
        self.level = level
    
    
    def tostring(self):
        return str(self.level) + " " + ("folder" if self.isdir else "file  ") + " " + self.path

def listdir(path, allfile, parent, level):
    level = level + 1
    filelist =  os.listdir(path)  
    for filename in filelist:  
        filepath = os.path.join(path, filename)

        isdir = False
        if os.path.isdir(filepath):
            isdir = True
            node = doc.createElement("folder")
            node.setAttribute("name", filename)
            node.setAttribute("path", filepath)
            node.setAttribute("level", str(level))
            parent.appendChild(node)
            listdir(filepath, allfile, node, level) 
        else:
            node = doc.createElement("file")
            node.setAttribute("name", filename)
            node.setAttribute("path", filepath)
            node.setAttribute("level", str(level))
            parent.appendChild(node)
        allfile.append(Node(filename, filepath, isdir, level))
    return allfile
def dump(files):
    print len(files)
    print "do something."
    for file in files:
        print file.tostring()
        
def putintodb(files):
    print "putintodb."
    

if __name__ == '__main__':
    files = []
    root = doc.createElement('root') #创建根元素
    doc.appendChild(root)
    listdir("/home/ubuntu/workspace/", files, root, 0)
    f = open('path.xml','w')
    # f.write(doc.toprettyxml(indent = ''))
    doc.writexml(f, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
    f.close()
    dump(files)
    putintodb(files)
    